import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Window extends JFrame implements GlobalConfig {

    private final Canvas canvas = new Canvas();

    public Window(String title) throws IOException {
        super(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setContentPane(canvas);
        setBounds(new Rectangle(WND_POS));
        pack();
        setResizable(false);
        setIconImage(ImageIO.read(new File("Sheep.png")));
    }

    private class Canvas extends JPanel {

        private final Background.BackGround back = new Background.BackGround();
        private BufferedImage snapshot;
        private Char.Sheep player;
        private Char.WolfCreator wolves;
        private boolean gaming = false;
        private int nowScore = 0;
        boolean messageBox = false;
        private final Fps.FpsCalc fps = new Fps.FpsCalc();
        private final JButton button = new JButton("开始");
        private final JLabel score = new JLabel();
        private final JLabel fpsCalc = new JLabel();
        private final JComboBox<String> choose = new JComboBox<>();

        public Canvas() throws IOException {
            super();
            setLayout(null);
            setPreferredSize(new Dimension(back.getSize().width+SZ_LIFT, back.getSize().height));
            setFocusable(true);
            addKeyListener(new ListenKey());
            addMouseMotionListener(new MouseMove());
            button.setBounds(CP_RECT_START);
            button.addActionListener(new Start());
            add(button);
            score.setBounds(CP_RECT_SCORE);
            add(score);
            choose.setBounds(CP_RECT_COMBO);
            choose.addItem("键盘");
            choose.addItem("鼠标");
            add(choose);
            fpsCalc.setBounds(CP_RECT_FPS);
            add(fpsCalc);
            Timer timer = new Timer(1000/FPS_SPEED, new FrameUpdate());
            timer.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(snapshot, 0, 0, null);
        }

        private class FrameUpdate implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                updateSnapshot();
                try {
                    fps.add();
                    if (gaming) {
                        player.update();
                        if (wolves.update(player)) {
                            messageBox = true;
                            gaming = false;
                            choose.setEnabled(true);
                        }
                    }
                } catch (IOException ex) {
                    System.out.println("别删我图片！");
                    System.exit(0);
                }
            }

            private void updateSnapshot() {     // 更新每一帧的画面
                BufferedImage frame = new BufferedImage(back.getSize().width, back.getSize().height, BufferedImage.TYPE_INT_ARGB);
                back.draw(frame);
                if (gaming) {
                    wolves.draw(frame);
                    player.draw(frame);
                    nowScore += (player.ctrlByKey()) ? SC_KEY : SC_MOUSE;
                } else {
                    Graphics2D g = (Graphics2D) frame.getGraphics();
                    g.setColor(TIC_CLR);
                    g.setFont(TIC_FONT);
                    g.drawString(TIC_TEXT, TIC_POS.x, TIC_POS.y);
                }
                score.setText("分数："+nowScore);
                fpsCalc.setText("帧率："+fps.getFps());
                snapshot = frame;
                repaint();
                if (messageBox) {
                    messageBox = false;
                    JOptionPane.showMessageDialog(canvas, score.getText(), "游戏结束", JOptionPane.PLAIN_MESSAGE);
                }
            }

        }

        private class ListenKey implements KeyListener {

            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                if (gaming) {
                    player.keyPress(e);
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (gaming) {
                    player.keyRelease(e);
                }
            }

        }

        private class Start implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    choose.setEnabled(false);
                    player = new Char.Sheep("键盘".equals(choose.getSelectedItem()), back.getSize());
                    wolves = new Char.WolfCreator(back.getSize());
                    gaming = true;
                    nowScore = 0;
                    button.setText("重新开始");
                    requestFocus();
                } catch (IOException ex) {
                    System.out.println("别删我图片！");
                    System.exit(0);
                }
            }

        }

        private class MouseMove implements MouseMotionListener {

            @Override
            public void mouseDragged(MouseEvent e) {

            }

            @Override
            public void mouseMoved(MouseEvent e) {
                if (gaming) {
                    player.mouseMove(e);
                }
            }

        }

    }

}
